package com.yonyou.iuap.kettle;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.pentaho.di.core.KettleEnvironment;
import org.pentaho.di.core.database.DatabaseMeta;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.trans.Trans;
import org.pentaho.di.trans.TransMeta;

/**
 * Servlet implementation class KltKettServlet
 */
@WebServlet("/KltKettServlet")
public class KltKettServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public KltKettServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		OutputStream out = response.getOutputStream();
		try {
			String result = pullData();
			out.write("SUCCESS".getBytes());
		} catch (KettleException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			out.write("ERROR".getBytes());
		}finally {
			out.flush();
			out.close();
		}
	}

	// // 获得文件绝对地址(项目绝对地址+我的文件相对项目的地址)
 	private String getFilePath() {
 		String absolute = this.getClass().getClassLoader().getResource("").getPath();
 		if (absolute.startsWith("/")) {
 			absolute = absolute.substring(1);
 		}
 		return absolute;
 	}
 	public String pullData() throws KettleException {
		String project_path = getFilePath();
		String filename = "kettle/pullKltDataToIncome.xml";
		filename = "/"+project_path + filename;
		// 调用转换
		String result = transfer(filename);
		return result;
	}
 
	public String transfer(String filename) throws KettleException {
		KettleEnvironment.init();
		
		TransMeta tm = new TransMeta(filename);
		Trans trans = new Trans(tm);
		// 空参调用
		trans.execute(null);
		trans.waitUntilFinished();
		//trans.setStopped(true);
		
		return trans.getResult().getLogText();
	}
	
    private static DatabaseMeta setKltDatabaseMeta() {
		return new DatabaseMeta(
				PropUtils.getConfigValByKey("klt.name"),
				PropUtils.getConfigValByKey("klt.type"), 
				PropUtils.getConfigValByKey("klt.access"),
				PropUtils.getConfigValByKey("klt.host"),
				PropUtils.getConfigValByKey("klt.db"),
				PropUtils.getConfigValByKey("klt.port"),
				PropUtils.getConfigValByKey("klt.user"), 
				PropUtils.getConfigValByKey("klt.pass"));
	}
	private static DatabaseMeta setOracleDatabaseMeta() {
		return new DatabaseMeta(
				PropUtils.getConfigValByKey("oracle.name"),
				PropUtils.getConfigValByKey("oracle.type"), 
				PropUtils.getConfigValByKey("oracle.access"),
				PropUtils.getConfigValByKey("oracle.host"),
				PropUtils.getConfigValByKey("oracle.db"),
				PropUtils.getConfigValByKey("oracle.port"),
				PropUtils.getConfigValByKey("oracle.user"), 
				PropUtils.getConfigValByKey("oracle.pass"));
	}
}
